<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册用户</title>
    <!-- 引入 Bootstrap CSS -->
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
    <style>
        /* 设置星空背景 */
        body {
            background: url('https://source.unsplash.com/1600x900/?stars,night') no-repeat center center fixed;
            background-size: cover;
            color: white; /* 调整字体颜色以在深色背景上可见 */
        }
        .container {
            background: rgba(0, 0, 0, 0.7); /* 半透明背景让内容更易读 */
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.5);
        }
    </style>
</head>
<body>
    <div class="container mt-5">
        <h2>注册用户</h2>
        <form id="addUser">
            <div class="form-group">
                <label for="userName">用户名</label>
                <input type="text" class="form-control" id="userName" name="userName" required>
            </div>
            <div class="form-group">
                <label for="userPassword">密码</label>
                <input type="text" class="form-control" id="userPassword" name="userPassword" required>
            </div>
            <div class="form-group">
                <label>用户身份</label><br>
                <div class="form-check">
                    <label for="userRole"></label>
                    <input class="form-check-input" type="radio" name="userRole" id="userRole" value="student" required>
                    <label class="form-check-label" for="roleStudent">
                        student
                    </label>
                </div>
                <div class="form-check">
                    <input class="form-check-input" type="radio" name="userRole" id="userRole" value="teacher" required>
                    <label class="form-check-label" for="roleTeacher">
                        teacher
                    </label>
                </div>
            </div>

            <button type="submit" class="btn btn-primary">提交</button>
        </form>
    </div>

    <!-- 使用 Fetch API 提交表单 -->
    <script>
        document.getElementById('addUser').addEventListener('submit', function(event) {
            event.preventDefault(); // 阻止表单的默认提交行为

            // 获取用户输入的数据
            const userName = document.getElementById('userName').value;
            const userPassword = document.getElementById('userPassword').value;
            const userRoles = document.getElementsByName('userRole');
            let selectedRole;

            for (const role of userRoles) {
                if (role.checked) {
                    selectedRole = role.value;
                    break;
                }
            }
            console.log("111111111111", userName);
            console.log("222222222222", userPassword);
            console.log("333333333333", userRole);
            // 使用 Fetch API 发送数据到后端
            fetch('/users/user/add_user', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json'
                },
                body: JSON.stringify({
                    userName: userName,
                    userPassword: userPassword,
                    userRole: selectedRole
                })
            })
            .then(response => response.json())
            .then(data => {
                if (data.error) {
                    alert(data.error);
                }
                if (data.message){
                    alert(data.message);
                    // 跳转到班级列表页面
                    window.location.href = '/users/users';
                }
            })
            .catch(error => {
                // 捕获并显示错误信息
                alert(error.message);
            });

        });
    </script>
</body>
</html>
